home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap13 / PopPad / PopPrnt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  6.0 KB  |  191 lines

  1. /*----------------------------------------------
  2.    POPPRNT.C -- Popup Editor Printing Functions
  3.   ----------------------------------------------*/
  4.  
  5. #include <windows.h>
  6. #include <commdlg.h>
  7. #include "resource.h"
  8.  
  9. BOOL bUserAbort ;
  10. HWND hDlgPrint ;
  11.  
  12. BOOL CALLBACK PrintDlgProc (HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  13. {
  14.      switch (msg)
  15.      {
  16.      case WM_INITDIALOG :
  17.           EnableMenuItem (GetSystemMenu (hDlg, FALSE), SC_CLOSE, MF_GRAYED) ;
  18.           return TRUE ;
  19.           
  20.      case WM_COMMAND :
  21.           bUserAbort = TRUE ;
  22.           EnableWindow (GetParent (hDlg), TRUE) ;
  23.           DestroyWindow (hDlg) ;
  24.           hDlgPrint = NULL ;
  25.           return TRUE ;
  26.      }
  27.      return FALSE ;
  28. }          
  29.  
  30. BOOL CALLBACK AbortProc (HDC hPrinterDC, int iCode)
  31. {
  32.      MSG msg ;
  33.      
  34.      while (!bUserAbort && PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  35.      {
  36.           if (!hDlgPrint || !IsDialogMessage (hDlgPrint, &msg))
  37.           {
  38.                TranslateMessage (&msg) ;
  39.                DispatchMessage (&msg) ;
  40.           }
  41.      }
  42.      return !bUserAbort ;
  43. }
  44.  
  45. BOOL PopPrntPrintFile (HINSTANCE hInst, HWND hwnd, HWND hwndEdit, 
  46.                        PTSTR szTitleName)
  47. {
  48.      static DOCINFO  di = { sizeof (DOCINFO) } ;
  49.      static PRINTDLG pd ;
  50.      BOOL            bSuccess ;
  51.      int             yChar, iCharsPerLine, iLinesPerPage, iTotalLines,
  52.                      iTotalPages, iPage, iLine, iLineNum ;
  53.      PTSTR           pstrBuffer ;
  54.      TCHAR           szJobName [64 + MAX_PATH] ;
  55.      TEXTMETRIC      tm ;
  56.      WORD            iColCopy, iNoiColCopy ;
  57.  
  58.           // Invoke Print common dialog box
  59.      
  60.      pd.lStructSize         = sizeof (PRINTDLG) ;
  61.      pd.hwndOwner           = hwnd ;
  62.      pd.hDevMode            = NULL ;
  63.      pd.hDevNames           = NULL ;
  64.      pd.hDC                 = NULL ;
  65.      pd.Flags               = PD_ALLPAGES | PD_COLLATE | 
  66.                               PD_RETURNDC | PD_NOSELECTION ;
  67.      pd.nFromPage           = 0 ;
  68.      pd.nToPage             = 0 ;
  69.      pd.nMinPage            = 0 ;
  70.      pd.nMaxPage            = 0 ;
  71.      pd.nCopies             = 1 ;
  72.      pd.hInstance           = NULL ;
  73.      pd.lCustData           = 0L ;
  74.      pd.lpfnPrintHook       = NULL ;
  75.      pd.lpfnSetupHook       = NULL ;
  76.      pd.lpPrintTemplateName = NULL ;
  77.      pd.lpSetupTemplateName = NULL ;
  78.      pd.hPrintTemplate      = NULL ;
  79.      pd.hSetupTemplate      = NULL ;
  80.      
  81.      if (!PrintDlg (&pd))
  82.           return TRUE ;
  83.      
  84.      if (0 == (iTotalLines = SendMessage (hwndEdit, EM_GETLINECOUNT, 0, 0)))
  85.           return TRUE ;
  86.  
  87.           // Calculate necessary metrics for file 
  88.      
  89.      GetTextMetrics (pd.hDC, &tm) ;
  90.      yChar = tm.tmHeight + tm.tmExternalLeading ;
  91.      
  92.      iCharsPerLine = GetDeviceCaps (pd.hDC, HORZRES) / tm.tmAveCharWidth ;
  93.      iLinesPerPage = GetDeviceCaps (pd.hDC, VERTRES) / yChar ;
  94.      iTotalPages   = (iTotalLines + iLinesPerPage - 1) / iLinesPerPage ;
  95.  
  96.           // Allocate a buffer for each line of text
  97.      
  98.      pstrBuffer = malloc (sizeof (TCHAR) * (iCharsPerLine + 1)) ;
  99.  
  100.           // Display the printing dialog box
  101.      
  102.      EnableWindow (hwnd, FALSE) ;
  103.      
  104.      bSuccess   = TRUE ;
  105.      bUserAbort = FALSE ;
  106.      
  107.      hDlgPrint = CreateDialog (hInst, TEXT ("PrintDlgBox"), 
  108.                                hwnd, PrintDlgProc) ;
  109.  
  110.      SetDlgItemText (hDlgPrint, IDC_FILENAME, szTitleName) ;
  111.      SetAbortProc (pd.hDC, AbortProc) ;
  112.  
  113.           // Start the document
  114.  
  115.      GetWindowText (hwnd, szJobName, sizeof (szJobName)) ;
  116.      di.lpszDocName = szJobName ;
  117.      
  118.      if (StartDoc (pd.hDC, &di) > 0)
  119.      {
  120.                // Collation requires this loop and iNoiColCopy
  121.  
  122.           for (iColCopy = 0 ;
  123.                iColCopy < ((WORD) pd.Flags & PD_COLLATE ? pd.nCopies : 1) ;
  124.                iColCopy++)
  125.           {
  126.                for (iPage = 0 ; iPage < iTotalPages ; iPage++)
  127.                {
  128.                     for (iNoiColCopy = 0 ;
  129.                          iNoiColCopy < (pd.Flags & PD_COLLATE ? 1 : pd.nCopies);
  130.                          iNoiColCopy++)
  131.                     {
  132.                               // Start the page
  133.  
  134.                          if (StartPage (pd.hDC) < 0)
  135.                          {
  136.                               bSuccess = FALSE ;
  137.                               break ;
  138.                          }
  139.  
  140.                               // For each page, print the lines
  141.                          
  142.                          for (iLine = 0 ; iLine < iLinesPerPage ; iLine++)
  143.                          {
  144.                               iLineNum = iLinesPerPage * iPage + iLine ;
  145.                               
  146.                               if (iLineNum > iTotalLines)
  147.                                    break ;
  148.                               
  149.                               *(int *) pstrBuffer = iCharsPerLine ;
  150.                               
  151.                               TextOut (pd.hDC, 0, yChar * iLine, pstrBuffer,
  152.                                        (int) SendMessage (hwndEdit, EM_GETLINE,
  153.                                        (WPARAM) iLineNum, (LPARAM) pstrBuffer));
  154.                          }
  155.                          
  156.                          if (EndPage (pd.hDC) < 0)
  157.                          {
  158.                               bSuccess = FALSE ;
  159.                               break ;
  160.                          }
  161.                          
  162.                          if (bUserAbort)
  163.                               break ;
  164.                     }
  165.                     
  166.                     if (!bSuccess || bUserAbort)
  167.                          break ;
  168.                }
  169.                
  170.                if (!bSuccess || bUserAbort)
  171.                     break ;
  172.           }
  173.      }
  174.      else
  175.           bSuccess = FALSE ;
  176.      
  177.      if (bSuccess)
  178.           EndDoc (pd.hDC) ;
  179.      
  180.      if (!bUserAbort)
  181.      {
  182.           EnableWindow (hwnd, TRUE) ;
  183.           DestroyWindow (hDlgPrint) ;
  184.      }
  185.      
  186.      free (pstrBuffer) ;
  187.      DeleteDC (pd.hDC) ;
  188.      
  189.      return bSuccess && !bUserAbort ;
  190. }
  191.